package com.cyq.dascma.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyq.dascma.constant.ValidaEnum;
import com.cyq.dascma.dto.ClazzInfoDto;
import com.cyq.dascma.dto.ScoreInfoDto;
import com.cyq.dascma.entity.Clazz;
import com.cyq.dascma.entity.Teacher;
import com.cyq.dascma.exception.ServiceException;
import com.cyq.dascma.handler.valida.impl.ClazzValidaImpl;
import com.cyq.dascma.result.Result;
import com.cyq.dascma.result.ResultGenerator;
import com.cyq.dascma.service.ClazzService;
import com.cyq.dascma.util.RequestUtil;
import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * dingwen
 * 2021/4/4 15:21
 **/
@Api(tags = "班级管理")
@RestController
@RequestMapping("/clazz")
public class ClazzController {
    private final ClazzService clazzService;

    @Autowired
    ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    /*
     *查询所有班级信息列表  info
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation(value = "查询所有班级信息列表  info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clazzId", value = "班级编号", required = false),
            @ApiImplicitParam(name = "clazzName", value = "班级名称", required = false),
            @ApiImplicitParam(name = "clazzYear", value = "年级", required = false),
            @ApiImplicitParam(name = "teacherId", value = "班主任教工号", required = false),
            @ApiImplicitParam(name = "teacherName", value = "班主任名称", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", required = true),
            @ApiImplicitParam(name = "pageNum", value = "第几页", required = true),
    })

    @GetMapping("info/page")
    public Result get(@RequestParam(value = "clazzId", required = false) String clazzId,
                      @RequestParam(value = "clazzName", required = false) String clazzName,
                      @RequestParam(value = "clazzYear", required = false) String clazzYear,
                      @RequestParam(value = "teacherId", required = false) String teacherId,
                      @RequestParam(value = "teacherName", required = false) String teacherName,
                      @RequestParam(value = "pageSize") Integer pageSize,
                      @RequestParam("pageNum") Integer pageNum, HttpServletRequest request) {
        Map<String, Object> params = RequestUtil.getParameterMap(request);
        params.put("offset", ((pageNum - 1) * pageSize));
        params.put("limit", (pageSize));
        List<ClazzInfoDto> clazzInfoList = clazzService.findClazzInfoList(params);
        return ResultGenerator.genOkResult(clazzInfoList);
    }

    /*
     *添加班级信息
     * @param [clazz]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("添加班级信息")
    @ApiImplicitParam(value = "班级实体", name = "clazz", required = true, dataType = "clazz", paramType = "body")
    @PostMapping("/add")
    public Result add(@RequestBody Clazz clazz) {
        try {
            new ClazzValidaImpl().valida(ValidaEnum.ADD, clazz);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }
        boolean flag = clazzService.save(clazz);
        return flag ? ResultGenerator.genOkResult(clazz) : ResultGenerator.genFailureResult("新增班级失败");
    }

    /*
     *更新班级信息
     * @param [clazz]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("更新班级信息")
    @ApiImplicitParam(value = "班级实体", name = "clazz", required = true, dataType = "clazz", paramType = "body")
    @PutMapping("/modify")
    public Result modify(@RequestBody Clazz clazz) {
        try {
            new ClazzValidaImpl().valida(ValidaEnum.MODIFY, clazz);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }
        boolean flag = clazzService.updateById(clazz);
        return flag ? ResultGenerator.genOkResult(clazz) : ResultGenerator.genFailureResult("更新班级失败");
    }

    /*
     * 根据班级编号删除班级信息
     * @param clazzId
     * @return Result
     */
    @ApiOperation("根据班级编号删除班级信息")
    @ApiImplicitParam(name = "clazzId", value = "班级编号", required = true, dataType = "String")
    @DeleteMapping("/remove/{clazzId}")
    public Result remove(@PathVariable("clazzId") String clazzId) {
        boolean flag = clazzService.removeById(clazzId);
        return flag ? ResultGenerator.genOkResult("ok") : ResultGenerator.genFailureResult("删除失败");
    }


    /*
     *查询所有班级的的ID & name & year 信息
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("查询所有班级的的ID & name & year 信息")
    @GetMapping("/ins")
    public Result get(){
        List<Clazz> clazzList = clazzService.list(new QueryWrapper<Clazz>().select("clazz_id","clazz_name","clazz_year"));
        return  ResultGenerator.genOkResult(clazzList);
    }

    /*
     *查询班级表总记录条数
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("查询班级表总记录条数")
    @GetMapping("/total")
    public Result getTotal() {
        return ResultGenerator.genOkResult(clazzService.count());
    }


}
